#!/usr/bin/env python

# To run the tests
#     TestVoting.py

# To document the tests
#     pydoc -w TestVoting

import Voting
import unittest

# ----------
# TestReader
# ----------
count =0
class TestReader (object) :
    def __init__ (self, s) :

        self.s = s.split('\n') # split around any new lines and make them into a list

    def read (self) :
        temp=""
        if(len(self.s)>0):
            temp =self.s[0] #tokenize strings by cutting off the first part of list and returning that part
            self.s=self.s[1:]
        return temp
        

# ----------
# TestWriter
# ----------

class TestWriter (object) :
    def str (self) :
        return self.s

    def write (self, *a) :
        self.s = str(a[0])
        for w in a[1:] :
            self.s += ' '
            self.s += str(w)
        self.s += '\n'

# -----------
# TestVoting
# -----------

class TestVoting (unittest.TestCase) :
    # ----
    # read
    # ----

    def test_read (self) :
        reader=TestReader("3\nJohn Doe\nJane Smith\nSirhan Sirhan\n1 2 3\n2 1 3\n1 2 3\n3 1 2")
        Voting.voting_read(reader)
        self.assert_(Voting.candidates ==  {0:"John Doe",1:"Jane Smith",2:"Sirhan Sirhan"})
        self.assert_(Voting.ballots ==  [['1','2','3'],['2','1','3'],['1','2','3'],['3','1','2']])
        self.assert_(Voting.votes == {0:[],1:[],2:[]})
    
    # ----
    # test eval
    # ----
    def test_eval1 (self):
        Voting.howmany =  1
        Voting.candidates = {0:"Mary",1:"Jane",2:"Watson"}
        Voting.ballots = [[1,2,3],[3,1,2],[3,2,1],[3,1,2]]
        Voting.votes = {0:[],1:[],2:[]}
        self.assert_(len(Voting.candidates) == 3)
        Voting.voting_eval()
        self.assert_(Voting.candidates.values()==['Watson'])
        self.assert_(Voting.won== True)

    def test_eval2(self):
        Voting.howmany =  1
        Voting.candidates = {0:"Jim"}
        Voting.ballots = [[1],[1]]
        Voting.votes = {0:[]}
        self.assert_(len(Voting.candidates) == 1)
        Voting.voting_eval()
        self.assert_(Voting.candidates.values()==['Jim'])
        self.assert_(Voting.won== True)

    def test_eval3 (self):
        Voting.howmany =  1
        Voting.candidates = {0:"Mary",1:"Jane",2:"Watson"}
        Voting.ballots = [[1,2,3],[1,2,3],[1,2,3],[3,1,2]]
        Voting.votes = {0:[],1:[],2:[]}
        self.assert_(len(Voting.candidates) == 3)
        Voting.voting_eval()
        self.assert_(Voting.candidates.values()==['Mary'])
        self.assert_(Voting.won== True)

    def test_eval4 (self):
        Voting.howmany =  1
        Voting.candidates = {0:"Peter",1:"Parker",2:"Spiderman"}
        Voting.ballots = [[3,1,2],[1,2,3],[3,2,1],[3,1,2],[3,2,1],[3,1,2],
                        [1,3,2],[2,1,3],[1,2,3],[1,3,2],[3,1,2],[3,2,1],
                        [1,2,3],[3,2,1],[1,3,2],[1,3,2],[2,1,3],[1,2,3]]
        Voting.votes = {0:[],1:[],2:[]}
        self.assert_(len(Voting.candidates) == 3)
        Voting.voting_eval()
        self.assert_(Voting.candidates.values()==['Peter'])
        self.assert_(Voting.won== True)

    def test_eval5 (self):
        Voting.howmany =  1
        Voting.candidates = {0:"Debra",1:"Rick",2:"Kay Bailey",3:"Bill"}
        Voting.ballots = [['1', '2', '3', '4'], ['2', '1', '3', '4'],
                        ['1', '2', '3', '4'], ['2', '1', '3', '4'],
                        ['3', '1', '2', '4'], ['4', '2', '1', '3']]
        Voting.votes = {0:[],1:[],2:[],3:[]}
        self.assert_(len(Voting.candidates) == 4)
        Voting.voting_eval()
        self.assert_(Voting.candidates.values()!=['Debra'])
        self.assert_(Voting.candidates.values()!=['Rick'])
        self.assert_(Voting.candidates.values()==['Debra','Rick'] or Voting.candidates.values()==['hasan','jalal'])
        self.assert_(Voting.won== True)


    # -----
    # test print
    # -----

    def test_print (self) :
        Voting.howmany =  1
        Voting.candidates = {0:"Debra",1:"Rick",2:"Kay",3:"Bill"}
        Voting.votes = {0:[],1:[],2:[],3:[]}
        writer = TestWriter()
        Voting.voting_print(writer)
        self.assert_(writer.str() == "['Debra', 'Rick', 'Kay', 'Bill']\n")

    def test_print2 (self) :
        Voting.howmany =  1
        Voting.candidates = {0:"Debra",1:"Rick",2:"Kay",3:"Bill"}
        Voting.ballots = [['1', '2', '3', '4'], ['2', '1', '3', '4'],
                        ['1', '2', '3', '4'], ['2', '1', '3', '4'],
                        ['3', '1', '2', '4'], ['4', '2', '1', '3']]
        Voting.votes = {0:[],1:[],2:[],3:[]}
        Voting.voting_eval()
        writer = TestWriter()
        Voting.voting_print(writer)
        self.assert_(writer.str() == "['Debra', 'Rick']\n")

if __name__ == "__main__" :
    unittest.main()
